home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / CBASE.ZIP / CBKEYPRE.C < prev    next >
Text File  |  1990-06-21  |  3KB  |  139 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbkeypre.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <stdlib.h>*/
  9. /*#include <string.h>*/
  10.  
  11. /* library headers */
  12. #include <blkio.h>
  13. #include <btree.h>
  14. #include <lseq.h>
  15.  
  16. /* local headers */
  17. #include "cbase_.h"
  18.  
  19. /*man---------------------------------------------------------------------------
  20. NAME
  21.      cbkeyprev - previous cbase key
  22.  
  23. SYNOPSIS
  24.      #include <cbase.h>
  25.  
  26.      int cbkeyprev(cbp, field)
  27.      cbase_t *cbp;
  28.      int field;
  29.  
  30. DESCRIPTION
  31.      The cbkeyprev function retreats the key cursor of the specified
  32.      field in cbase cbp by one.  The record cursor of cbp is
  33.      positioned to the record associated with that key.  Other key
  34.      cursors are not affected.
  35.  
  36.      cbkeyprev will fail if one or more of the following is true:
  37.  
  38.      [EINVAL]       cbp is not a valid cbase pointer.
  39.      [EINVAL]       field is not a valid field number for
  40.                     cbase cbp.
  41.      [CBELOCK]      cbp is not locked.
  42.      [CBENKEY]      field is not a key.
  43.      [CBENOPEN]     cbp is not open.
  44.  
  45. SEE ALSO
  46.      cbkcursor, cbkeyfirst, cbkeylast, cbkeynext, cbrecprev.
  47.  
  48. DIAGNOSTICS
  49.      Upon successful completion, a value of 0 is returned.  Otherwise,
  50.      a value of -1 is returned, and errno set to indicate the error.
  51.  
  52. ------------------------------------------------------------------------------*/
  53. int cbkeyprev(cbp, field)
  54. cbase_t *cbp;
  55. int field;
  56. {
  57.     void *buf = NULL;
  58.     cbrpos_t cbrpos = NIL;
  59.     lspos_t lspos = NIL;
  60.  
  61.     /* validate arguments */
  62.     if (!cb_valid(cbp)) {
  63.         errno = EINVAL;
  64.         return -1;
  65.     }
  66.  
  67.     /* check if not open */
  68.     if (!(cbp->flags & CBOPEN)) {
  69.         errno = CBENOPEN;
  70.         return -1;
  71.     }
  72.  
  73.     /* validate arguments */
  74.     if (field < 0 || field >= cbp->fldc) {
  75.         errno = EINVAL;
  76.         return -1;
  77.     }
  78.  
  79.     /* check if field not a key */
  80.     if (!(cbp->fldv[field].flags & CB_FKEY)) {
  81.         errno = CBENKEY;
  82.         return -1;
  83.     }
  84.  
  85.     /* check if not locked */
  86.     if (!(cbp->flags & CBLOCKS)) {
  87.         errno = CBELOCK;
  88.         return -1;
  89.     }
  90.  
  91.     /* set key cursor to previous key */
  92.     if (btprev(cbp->btpv[field]) == -1) {
  93.         CBEPRINT;
  94.         return -1;
  95.     }
  96.  
  97.     /* check if cursor is null */
  98.     if (btcursor(cbp->btpv[field]) == NULL) {
  99.         /* set record cursor to null */
  100.         if (lssetcur(cbp->lsp, NULL) == -1) {
  101.             CBEPRINT;
  102.             return -1;
  103.         }
  104.         errno = 0;
  105.         return 0;
  106.     }
  107.  
  108.     /* get record position */
  109.     if (btkeysize(cbp->btpv[field]) != (cbp->fldv[field].len + sizeof(cbrpos_t))) {
  110.         CBEPRINT;
  111.         errno = CBEPANIC;
  112.         return -1;
  113.     }
  114.     buf = calloc((size_t)1, btkeysize(cbp->btpv[field]));
  115.     if (buf == NULL) {
  116.         CBEPRINT;
  117.         errno = ENOMEM;
  118.         return -1;
  119.     }
  120.     if (btgetk(cbp->btpv[field], buf) == -1) {
  121.         CBEPRINT;
  122.         free(buf);
  123.         return -1;
  124.     }
  125.     memcpy(&cbrpos, ((char *)buf + cbp->fldv[field].len), sizeof(cbrpos));
  126.     free(buf);
  127.     buf = NULL;
  128.  
  129.     /* set record cursor */
  130.     lspos = cbrpos;
  131.     if (lssetcur(cbp->lsp, &lspos) == -1) {
  132.         CBEPRINT;
  133.         return -1;
  134.     }
  135.  
  136.     errno = 0;
  137.     return 0;
  138. }
  139.